<?php
/**
 * User: taodf
 * Date: 17-4-29
 * Time: 下午8:27
 */

namespace Zeus\Routing;

use Closure;
use League\Container\ContainerInterface as Container;
use Zeus\Routing\Exceptions\RouteException;

/**
 * 路由类
 *
 * Class Router
 * @package Zeus\Routing
 */
class Router
{
    /**
     * 路由引擎
     *
     * @var RouterInterface $engine
     */
    private $engine;

    /**
     * @var Container
     */
    private $app;

    public function __construct(Container $app, $name="FastRoute")
    {
        $this->app=$app;
        $this->engine=RouterFactory::createRouter($name);
    }

    /**
     * 向路由引擎中注册路由
     *
     * @param string|array $method
     * @param string $url
     * @param callable $callable
     */
    public function register($method, $url, $callable)
    {
        $this->engine->register($method, $url, $callable);
    }

    /**
     * 注册GET方法路由
     *
     * @param string $url
     * @param callable $callable
     */
    public function get($url, $callable)
    {
        $this->engine->get($url, $callable);
    }

    /**
     * 注册POST方法路由
     *
     * @param string $url
     * @param callable $callable
     */
    public function post($url, $callable)
    {
        $this->engine->post($url, $callable);
    }

    /**
     * 注册PUT方法路由
     *
     * @param string $url
     * @param callable $callable
     */
    public function put($url, $callable)
    {
        $this->engine->put($url, $callable);
    }

    /**
     * 注册PATCH方法路由
     *
     * @param string $url
     * @param callable $callable
     */
    public function patch($url, $callable)
    {
        $this->engine->patch($url, $callable);
    }

    /**
     * 注册DELETE方法路由
     *
     * @param string $url
     * @param callable $callable
     */
    public function delete($url, $callable)
    {
        $this->engine->delete($url, $callable);
    }

    /**
     * 注册HEAD方法路由
     *
     * @param string $url
     * @param callable $callable
     */
    public function head($url, $callable)
    {
        $this->engine->head($url, $callable);
    }

    /**
     * 分组注册路由
     *
     * @param string $prefix
     * @param callable $callable
     */
    public function group($prefix, $callable)
    {
        $this->engine->group($prefix, $callable);
    }

    /**
     * 注册所有方法类型的路由
     *
     * @param string $url
     * @param callable $callable
     */
    public function all($url, $callable)
    {
        $this->engine->get($url, $callable);
        $this->engine->post($url, $callable);
        $this->engine->delete($url, $callable);
        $this->engine->head($url, $callable);
        $this->engine->patch($url, $callable);
        $this->engine->put($url, $callable);
    }

    /**
     * @param \Zeus\Http\Request $request
     * @return string
     */
    public function dispatch($request)
    {
        $result="";
        $this->engine->dispatch($request, function($handler, $parameter)use(&$result){
            if(false===$handler || false===$parameter){
                throw new RouteException();
            }

            if($handler instanceof Closure){
                $result=$this->app->call($handler, $parameter);
            }else{
                $segments=explode("@", $handler);
                $result=$this->app->call([
                    new $segments[0]($this->app, $this->app->get("view")), $segments[1]
                ], $parameter);
            }
        });
        return $result;
    }

}